package com.scaffold.controller.impl;

import com.scaffold.client.impl.CaffeineCacheClient;
import com.scaffold.controller.CacheApi;
import com.scaffold.model.domain.UserDO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Map;

/**
 * @author Da Shuai
 * @since 2021-08-30 16:21:10
 */
@RestController
@RequiredArgsConstructor
public class CacheController implements CacheApi {

    private final CaffeineCacheClient caffeineCacheClient;

    @Override
    public void init() {
        for (int i = 0; i < 5; i++) {
            UserDO userDO = new UserDO();
            userDO.setId((long) i);
            userDO.setName("这是id为" + i + "的用户的姓名");
            caffeineCacheClient.put(i + "", userDO);
        }
    }

    @Override
    public Object get(String key) {
        return caffeineCacheClient.get(key);
    }

    @Override
    public Map<String, Object> listValue() {
        return caffeineCacheClient.map();
    }

    @Override
    public Collection<String> listKey() {
        return caffeineCacheClient.listKey();
    }

    @Override
    public String getStats() {
        return caffeineCacheClient.getStats();
    }
}
